﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PopShotFlashController : MonoBehaviour {

	private Animator anim;


	// singleton list to hold all our projectiles
	static private List<PopShotFlashController> popShotFlashControllers;


	//--------------------------------------------------------------------------
	// static public methods
	//--------------------------------------------------------------------------
	static public void Reset()
	{
		// for each projectile
		foreach(PopShotFlashController popShotFlashController in popShotFlashControllers)
		{
			// configure it
			popShotFlashController.Configure();
		}
	}

	static public PopShotFlashController Spawn(Vector3 location)
	{
		// search for the first free popShotImpactController
		foreach(PopShotFlashController popShotFlashController in popShotFlashControllers)
		{
			// if disabled, then it's available
			if(popShotFlashController.gameObject.activeSelf == false)
			{
				// set it up
				popShotFlashController.transform.position = location;
				popShotFlashController.gameObject.SetActive(true);

				// return a reference to the caller
				return popShotFlashController;
			}
		}
		// we normally wouldn't get here unless the player is firing rapidly
		// in some games we always want the pool request to succeed, if so
		// then we would instantiate another one here if we needed
		// in our case, it's part of the design that you can only have
		// a limited number of shots active at any one time
		return null;
	}

	//--------------------------------------------------------------------------
	// protected mono methods
	//--------------------------------------------------------------------------
	protected void Awake()
	{
		// does the pool exist yet?
		if(popShotFlashControllers == null)
		{
			// lazy initialize it
			popShotFlashControllers = new List<PopShotFlashController>();
		}
		// add myself
		popShotFlashControllers.Add(this);
	}

	protected void OnDestroy()
	{
		// remove myself from the pool
		popShotFlashControllers.Remove(this);
		// was I the last one?
		if(popShotFlashControllers.Count == 0)
		{
			// remove the pool itself
			popShotFlashControllers = null;
		}
	}

	protected void Start()
	{
		anim = GetComponent<Animator>();
		gameObject.SetActive(false);
	}

	protected void Update()
	{
		//Check to see if animation is done playing
		if(anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
		{
			gameObject.SetActive(false);
		}
		else
		{
			//Find the Player GunPoint position and update this GameObject position to follow it.
			GameObject player = GameObject.FindWithTag("Player");
			Transform gunPoint = player.transform.FindChild("GunPoint");
			gameObject.transform.position = gunPoint.position;
		}


	}


	//--------------------------------------------------------------------------
	// private methods
	//--------------------------------------------------------------------------
	private void Configure()
	{
		// just disable myself so I'm available for use
		gameObject.SetActive(false);
	}


}

